﻿
using Microsoft.AspNetCore.Server.Kestrel.Internal.System.Collections.Sequences;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Algorithm_api.calculate.StrongLine
{
    public class Graph
    {
        public int size { get; set; }
        public int[][] edge { get; set; }
        public ArrayList<string> vtcName { get; set; }
        public bool[] visited { get; set; }

        public Graph(int size)
        {
            this.size = size;
            edge = new int[size][];
            for (int i = 0; i < size; i++) edge[i] = new int[size];
            vtcName = new ArrayList<string>();
            visited = new bool[size];
        }

        public void ResetVisited()
        {
            for(int i = 0; i < size; i++)
            {
                visited[i] = false;
            }
        }

        public void AddVertex(string v)
        {
            vtcName.Add(v);
        }

        public bool AddEdge(string from,string to)
        {
            int u = GetVIndex(from);
            int v = GetVIndex(to);

            if (v == -1 || u == -1) return false;
            edge[u][v] = 1;
            return true;
        }

        public int GetVIndex(string wh)
        {
            for(int i = 0; i < size; i++)
            {
                if (wh == vtcName[i]) return i;
            }
            return -1;
        }

        public void dfs(int start, ArrayList<int> discovered,ArrayList<int> finished)
        {
            visited[start] = true;
            if (discovered != null)
            {
                discovered.Add(start);
            }
            for (int i = 0; i < size; i++)
            {
                if (edge[start][i] != 0 && visited[i] == false)
                {
                    dfs(i, discovered, finished);
                }
            }
            if (finished != null)
            {
                finished.Add(start);
            }
        }
    }
}
